<script type="text/javascript">
//商品列表
$(function() {
$('#dg').datagrid({
    url: 'Goods/data_list',
    fit: true,
    fitColumns: true,
    border: false,
    pagination: true,
    idField: 'id',
    pageSize: 10,
    pageList: [10, 20, 30, 40, 50],
    sortName: 'createdatetime',
    sortOrder: 'desc',
    checkOnSelect: false,
    selectOnCheck: false,
    nowrap: false,
    frozenColumns: [[{
        title: '商品编号',
        field: 'sku',
        width: 70,
        sortable: true
    }]],
    columns: [[{
        title: '产品图片',
        field: 'picture',
        width: 50,
        formatter: function(picture) {
            return "<a target='_blank' href='uploads/goods/" + picture + "'><img src='uploads/goods/" + picture + "' width='50' height='50' /></a>";
        }
    },
    {
        title: '标题',
        field: 'title',
        width: 420
    },
    {
        title: '采购价',
        field: 'purchasing_price',
        width: 40
    },
    {
        title: '零售价',
        field: 'retail_price',
        width: 40
    },
    {
        title: '库存',
        field: 'stock',
        width: 40
    },
    {
        field: 'date',
        title: '日期',
        width: 55
    },
    {
        field: 'action',
        title: '动作',
        width: 100,
        formatter: function(value, row, index) { //return formatString('<a iconCls="icon-add" onclick="editGoods(\'{0}\');">编辑</a>&nbsp;<a href="javascript:void(0)"  onclick="destroyGoods(\'{0}\');">删除</a>',row.id,row.id);
            return formatString('<img style="cursor:pointer" onclick="editGoods(\'{0}\');" src="{1}"/>&nbsp;<img style="cursor:pointer" onclick="destroyGoods(\'{2}\');" src="{3}"/>', row.id, 'Resource/images/pencil.png', row.id, 'Resource/images/cancel.png');
        }
    }]],
    toolbar: '#toolbar1'
});
});
//新增商品
function newGoods() {
$('#dg').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
$('<div/>').dialog({
    href: 'Goods/add',
    width: 800,
    height: 500,
    modal: true,
    title: '商品详情',
    buttons: [{
        text: '增加',
        iconCls: 'icon-add',
        handler: function() {
            var d = $(this).closest('.window-body');
            $('#goods-add').form({
                url: 'Goods/data_save',
                success: function(result) {
                    $("#dg").datagrid('reload');
                    d.dialog('destroy');
                }
            });
            $('#goods-add').submit();
        }
    }],
    onClose: function() {
        $(this).dialog('destroy');
    }
});
}
function supplies_add(){
	$('<div/>').dialog({
		href: './Supplier/supplier_list',
	    width: 600,
	    height: 400,
	    modal: true,
	    title: '从供应商中添加',
	    buttons:[{
	       text: '保存',
	       iconCls: 'icon-save'
	    }]
	})
}
function editGoods(id) {
$('#dg').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
$('<div/>').dialog({
    href: 'Goods/edit?id=' + id,
    width: 800,
    height: 500,
    modal: true,
    title: '商品详情',
    buttons: [{
        text: '修改',
        iconCls: 'icon-edit',
        handler: function() {
            var d = $(this).closest('.window-body');
            $('#goods-edit').form({
                url: 'Goods/data_update?id=' + id,
                success: function(result) {
                	$("#dg").datagrid('reload');
                    d.dialog('destroy');
                }
            });
            $('#goods-edit').submit();
        }
    }],
    onClose: function() {
        $(this).dialog('destroy');
    },
    onLoad: function() {}
});
}
function destroyGoods(id) {
$('#dg').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
    $.messager.confirm('确认框', '你确定要删除这个商品吗？',
    function(r) {
        if (r) {
            $.post('Goods/data_remove?id=' + id,
            function(result) {
                if (result.success) {
                    $('#dg').datagrid('reload'); // reload the user data  
                } else {
                    $.messager.show({ // show error message  
                        title: 'Error',
                        msg: result.errorMsg
                    });
                }
            },
            'json');
        }
    });
}
</script>
<table id="dg">
</table>
<div id="toolbar1">
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onClick="newGoods()">增加</a>
</div>

